%main = getbonds(main)
%   This function determines all the bonds between the elements and the
%   type of those bonds with paying attention to rings and branches.

%%

function main = getBonds(main)

%% Initialising
%Creating the field 'bonds' in main to ensure output and a working field
%for the function to work with
main(1).bonds = [];


%% Main

%% Bonds between ringmarkers
%For all elements but the last in 'main':
for j = 1:length(main)-1
    %Check whether the element carries a ringmarker
    if ~isempty(main(j).ringmarker)
        %If it does, check 'main' for the corrosponding ringmarker
        for k = 1:length(main)
            %If there is only one ringmarker at 'j'
            if length(main(j).ringmarker) == 1
                %See if any ringmarker at 'k' corrosponds
                if sum([main(k).ringmarker] == main(j).ringmarker)
                    %If they do and the current 'j' and 'k' are not the
                    %same store 'k' and break
                    if j ~= k
                        kstor = k;
                        break
                    end
                end
            else
                %If position 'j' has more than one ringmarker see if the
                %ringmarker at 'k' corrosponds with the any of the
                %ringmarkers at 'j'
                if sum([main(j).ringmarker] == main(k).ringmarker)
                    %If they do and the current 'j' and 'k' are not the
                    %same position store 'k' and break
                    if j ~= k
                        kstor = k;
                        break
                    end
                end
                
            end
        end
        %After finding the corrosponding position, add it to the field
        %bonds in which all positions that are bonded to 'j' are stored.
        main(j).bonds = [main(j).bonds kstor];
    end
end

%% Branchorigins, hasBranch and bonds within branches
%Run the function 'getBranches' to gain the info on the origins of each
%branch and to bind the elements within the branches. It also adds the
%first members of every branch but the main branch to the field 'hasBranch'
%of the branches origin.
main = getBranches(main);

%% Bonding first of branch to the branchorigin
%Predefine 'done'
done = zeros(length(main),1);

%For all elements in 'main'
for j = 1:length(main)
    %If 'j' has a branchorigin and it's branch has not been stored in
    %'done' yet
    if main(j).branchorigin && isempty(find(done == main(j).branch,1))
        %'j' Is bonded to it's branchorigin
        main(j).bonds = [main(j).bonds main(j).branchorigin];
        %If 'j' has a bondtype
        if ~isempty(main(j).bondtype)
            %Add a tenth of it's bondtype to the just created bond
            main(j).bonds(length(main(j).bonds)) = ...
                main(j).bonds(length(main(j).bonds)) + main(j).bondtype/10;
        end
        %And add it's branchnumber to 'done'
        done(j) = main(j).branch;
    end
end

%% Adding bondtypes to the bonds
%For all elements in 'main'
for j = 1:length(main)
    %For all bonds that element 'j' has
    for k = 1:length(main(j).bonds)
        %If the bond does not yet have a bondtype attached
        if main(j).bonds(k) == floor(main(j).bonds(k))
            %If it has to have an aromatic bondtype add .15 to the bonds
            if main(j).aromatic && main(floor(main(j).bonds(k))).aromatic
                main(j).bonds(k) = main(j).bonds(k) + .15;
                %If is has another bondtype add a tenth of that type to the
                %bonds
            elseif ~isempty(main(floor(main(j).bonds(k))).bondtype)
                main(j).bonds(k) = main(j).bonds(k) + ...
                    main(floor(main(j).bonds(k))).bondtype/10;
                %Else just add .1 to label it single bonded
            else
                main(j).bonds(k) = main(j).bonds(k) + .1;
            end
        end
    end
end

%% Crossbonding, sorting and seperating the bonds and the bondtypes
%For all elements in 'main'
for j = 1:length(main)
    %For all its bonds
    for k = 1:length(main(j).bonds)
        %Bond 'j' to all elements in its field 'bonds'
        main(floor(main(j).bonds(k))).bonds = ...
            [main(floor(main(j).bonds(k))).bonds ...
            j+main(j).bonds(k)-floor(main(j).bonds(k))];
    end
end

%Sorting the bonds
for j = 1:length(main)
    main(j).bonds = sort(main(j).bonds);
end
%For all elements in 'main'
for j = 1:length(main)
    %Start at second position
    k = 2;
    %As long as 'k' does not reach past the end of the field 'bonds'
    while k <= length(main(j).bonds)
        %If the previous element is also a bond to the same atom, clear
        %this one.
        if floor(main(j).bonds(k)) == floor(main(j).bonds(k-1))
            main(j).bonds(k) = [];
        else
            %Else go on to the next 'k'
            k = k+1;
        end
    end
end

%For all bonds
for j = 1:length(main)
    for k = 1:length(main(j).bonds)
        %Put the bondtypes into the field 'bondstype' per element and
        main(j).bondstype(k) = ...
            round(100*(main(j).bonds(k)-floor(main(j).bonds(k))))/10;
        %Remove the bondtype label from the element 'k' in 'bonds'
        main(j).bonds(k) = floor(main(j).bonds(k));
    end
end

%% Coordination
%Get the coordination per element in 'main' by using 'getCoordination'
main = getCoordination(main);

end